//Copyright 2013 feelinggo.com All Rights Reserved
/**
 * feelinggo
 * created by author kiwind(site:kiwind.cn) @ 2013-04-27
 * 页面级方法
 */
 var FGGO = FGGO || {};
FGGO.view = {
	init:function(){
		this.sidebar.init();
		this.submenu();
		this.submenu({
			box : "#aboutMenu",
			tweenWidth : 92,
			iconOriginalMarginLeft : 8,
			iconMarginLeft : 3
		});
		this.scrollFn();
	},
	sidebar:{
		init:function(opt){
			this.opts = opt || {};
			this.box = this.opts.box || ".sidebar";
			this.btnEl = this.opts.btnEl || "a";
			this.toggleClass = this.opts.toggleClass || "on";
			this.label = this.opts.label || ".label";
			this.bind();
		},
		bind:function(){
			var _this = this,
				_box = $(_this.box);
			_box.find(_this.btnEl).bind({
				mouseover:function(){
					_this.overHandle($(this))
				},
				click:function(){
					_this.clickHandle($(this));
				},
				mouseout:function(){
					_this.outHandle($(this));
				}
			});
		},
		overHandle:function($this){
			$this.addClass(this.toggleClass);
			$this.next().show();
		},
		outHandle:function($this){
			$this.removeClass(this.toggleClass);
			$this.next().hide();
		},
		clickHandle:function($this){
			var _index = $this.parent().index();
			if(_index != 2)
			{
				$(document.body).animate({scrollTop:_index * 700});
			}
		}
	},
	submenu:function(opt){
		var defaults = {
			box : "#workMenu",
			tabCon: ".work-tab-con",
			count : ".work-count",
			countTxt : ".work-count-txt",
			btnEl : "li",
			toggleClass : "on",
			icon : ".icon-work",
			txtEl : "span",
			speed : 200,
			originalWidth : 42,
			tweenWidth : 82,
			iconOriginalMarginLeft : 14,
			iconMarginLeft : 8
		}	
		var opts = $.extend({},defaults,opt);
		bind();

		function bind(){
			var _box = $(opts.box);

			_box.find(opts.btnEl).bind({
				mouseenter:function(){
					if(!$(this).hasClass("on"))
					{
						enterHandle($(this));
					}
				},
				mouseleave:function(){
					if(!$(this).hasClass("on"))
					{
						leaveHandle($(this));
					}
				},
				click:function(){
					clickHandle($(this));
				}
			});
		}

		function enterHandle($this){
			$this.stop().animate({width : opts.tweenWidth}, opts.speed);
			$this.find(opts.icon).stop().animate({marginLeft:opts.iconMarginLeft}, opts.speed);
			$this.find(opts.txtEl).removeClass("hide");
		}
		function leaveHandle($this){
			$this.stop().animate({width : opts.originalWidth}, opts.speed);
			$this.find(opts.icon).stop().animate({marginLeft:opts.iconOriginalMarginLeft}, opts.speed);
			$this.find(opts.txtEl).addClass("hide");
		}
		function clickHandle($this){
			var tmpEl = $(opts.box).find(opts.btnEl + "." +opts.toggleClass).css({"background-color":"#999"});
			tmpEl.animate({
				width : opts.originalWidth
			}, opts.speed, function(){
				tmpEl.removeClass(opts.toggleClass);
			}).bind("mouseleave", function(){
				leaveHandle($(this));
			});
			tmpEl.find(opts.icon).animate({marginLeft:opts.iconOriginalMarginLeft}, opts.speed);
			tmpEl.find(opts.txtEl).addClass("hide");
			var countTmp = tmpEl.find(opts.countTxt).clone();
			tmpEl.find(opts.countTxt).remove();
			$this.removeAttr("style").addClass(opts.toggleClass);
			$this.unbind("mouseleave");
			$this.append(countTmp);
			$(opts.tabCon).not($this.index()).hide();
			$(opts.tabCon).eq($this.index()).show();
		}
	},
	scrollFn:function(){
		bind();
		function bind(){
			var _index = $(".index"),
				_work = $(".work"),
				_about = $(".about"),
				_indexTop = _index.offset().top,
				_workTop = _work.offset().top,
				_aboutTop = _about.offset().top;
				_h = $(window).height() - 300,
				_sidebarItem = $("#sidebar").find("li");
			$(window).bind("scroll",function(){
				var _top = $(this).scrollTop();
				if((_workTop - _top) < _h)
				{
					_work.find(".content-item-box").fadeIn();
					_sidebarItem.find("a").removeClass("cur");
					_sidebarItem.eq(1).find("a").addClass("cur");
				}
				else
				{
					_work.find(".content-item-box").fadeOut();
				}
				if((_aboutTop - _top) < _h)
				{
					_sidebarItem.find("a").removeClass("cur");
					_sidebarItem.eq(3).find("a").addClass("cur");
					_about.find(".content-item-box").fadeIn();
				}
				else
				{
					_about.find(".content-item-box").fadeOut();
				}
				if(_top === 0)
				{
					_sidebarItem.find("a").removeClass("cur");
					_sidebarItem.eq(0).find("a").addClass("cur");
				}
			});
		}
	},
	arrow:function(){
		$(".arrow").bind({
			mouseover:function(){
				$(this).addClass("on")
			}
		});
	}
};

$(function(){
	FGGO.view.init();
})